package cn.com.gzheroli.core.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanPredicate;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
import org.apache.commons.collections.functors.EqualPredicate;
import org.apache.commons.lang.StringUtils;

import cn.com.gzheroli.bfw.pojo.base.TSFunction;

/**
 * 动态菜单栏生成
 * 
 * 
 */
public class ListtoMenu {
	/**
	 * 拼装easyui菜单JSON方式
	 * 
	 * @param set
	 * @param set1
	 * @return
	 */
	public static String getMenu(List<MenuBean> set, List<MenuBean> set1) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"menus\":[");
		for (MenuBean node : set) {
			String iconClas = "default";// 权限图标样式
			if (node.getIconId() != null) {
				iconClas = node.getIconClas();
			}
			buffer.append("{\"menuid\":\"" + node.getNo() + "\",\"icon\":\""
					+ iconClas + "\"," + "\"menuname\":\""
					+ node.getName() + "\",\"menus\":[");
			iterGet(set1, node.getNo(), buffer);
			buffer.append("]},");
		}
		buffer.append("]}");

		// 将,\n]替换成\n]

		String tmp = buffer.toString();

		tmp = tmp.replaceAll(",\n]", "\n]");
		tmp = tmp.replaceAll(",]}", "]}");
		return tmp;

	}

	static int count = 0;

	/**
	 * @param args
	 */

	static void iterGet(List<MenuBean> set1, String pid, StringBuffer buffer) {

		for (MenuBean node : set1) {

			// 查找所有父节点为pid的所有对象，然后拼接为json格式的数据
			count++;
			if (node.getParentId().equals(pid))

			{
				buffer.append("{\"menuid\":\"" + node.getNo()
						+ " \",\"icon\":\"" + node.getIconClas()
						+ "\"," + "\"menuname\":\"" + node.getName()
						+ "\",\"url\":\"" + node.getUrl() + "\"");
				if (count == set1.size()) {
					buffer.append("}\n");
				}
				buffer.append("},\n");

			}
		}

	}

	/**
	 * 拼装Bootstarp菜单
	 * 
	 * @param pFunctions
	 * @param functions
	 * @return
	 */
	public static String getBootMenu(List<MenuBean> pFunctions,List<MenuBean> functions) {
		StringBuffer menuString = new StringBuffer();
		menuString.append("<ul>");
		for (MenuBean pFunction : pFunctions) {
			menuString
					.append("<li><a href=\"#\"><span class=\"icon16 icomoon-icon-stats-up\"></span><b>"
							+ pFunction.getName() + "</b></a>");
			int submenusize=pFunction.getChildren().size()==0?0:1;
			if (submenusize == 0) {
				menuString.append("</li>");
			}
			if (submenusize > 0) {
				menuString.append("<ul class=\"sub\">");
			}
			for (MenuBean function : functions) {

				if (function.getParentId().equals(pFunction.getNo())) {
					menuString
							.append("<li><a href=\""
									+ function.getUrl()
									+ "\" target=\"contentiframe\"><span class=\"icon16 icomoon-icon-file\"></span>"
									+ function.getName() + "</a></li>");
				}
			}
			if (submenusize > 0) {
				menuString.append("</ul></li>");
			}
		}
		menuString.append("</ul>");
		return menuString.toString();

	}

	/**
	 * 拼装EASYUI菜单
	 * 
	 * @param pFunctions
	 * @param functions
	 * @return
	 */
	public static String getEasyuiMenu(List<MenuBean> pFunctions,List<MenuBean> functions) {
		StringBuffer menuString = new StringBuffer();
		for (MenuBean pFunction : pFunctions) {
			menuString.append("<div  title=\"" + pFunction.getName()
					+ "\" iconCls=\"" + pFunction.getShortcutClass()
					+ "\">");
			int submenusize=pFunction.getChildren().size()==0?0:1;
			if (submenusize == 0) {
				menuString.append("</div>");
			}
			if (submenusize > 0) {
				menuString.append("<ul>");
			}
			for (MenuBean function : functions) {

				if (function.getParentId().equals(pFunction.getNo())) {
					String icon = "folder";
					if (function.getIconId() != null) {
						icon = function.getIconClas();
					}
					// menuString.append("<li><div> <a class=\""+function.getFunctionName()+"\" iconCls=\""+icon+"\" target=\"tabiframe\"  href=\""+function.getFunctionUrl()+"\"> <span class=\"icon "+icon+"\" >&nbsp;</span> <span class=\"nav\">"+function.getFunctionName()+"</span></a></div></li>");
					menuString.append("<li><div onclick=\"addTab(\'"
							+ function.getName() + "\',\'"
							+ function.getUrl() + "&clickFunctionId="
							+ function.getNo() + "\',\'" + icon
							+ "\')\"  title=\"" + function.getName()
							+ "\" url=\"" + function.getUrl()
							+ "\" iconCls=\"" + icon + "\"> <a class=\""
							+ function.getName()
							+ "\" href=\"#\" > <span class=\"icon " + icon
							+ "\" >&nbsp;</span> <span class=\"nav\" >"
							+ function.getName()
							+ "</span></a></div></li>");
				}
			}
			if (submenusize > 0) {
				menuString.append("</ul></div>");
			}
		}
		return menuString.toString();

	}

	/**
	 * 拼装EASYUI 多级 菜单
	 * 
	 * @param pFunctions
	 * @param functions
	 * @return
	 */
	public static String getEasyuiMultistageMenu(List<MenuBean> list) {
		StringBuffer menuString = new StringBuffer();
		for (MenuBean function : list) {
			menuString.append("<div   title=\"" + function.getName()
					+ "\" iconCls=\"" + function.getIconClas()
					+ "\">");
			//int submenusize = function.getTSFunctions().size();
			int submenusize=function.getChildren().size()==0?0:1;
			if (submenusize == 0) {
				menuString.append("</div>");
			}
			if (submenusize > 0) {
				menuString.append("<ul>");
			}
			menuString.append(getChild(function.getChildren()));
			if (submenusize > 0) {
				menuString.append("</ul></div>");
			}
		}
		return menuString.toString();
	}
	/**
	 * 拼装EASYUI 多级 菜单  下级菜单为树形
	 * 
	 * @param pFunctions
	 * @param functions
	 * @return
	 */
	public static String getEasyuiMultistageTree(List<MenuBean> list) {
		if(list==null||list.size()==0){return "不具有任何权限,\n请找管理员分配权限";}
		StringBuffer menuString = new StringBuffer();
		for (MenuBean function : list) {
			/*menuString.append("<div   title=\"" + function.getFunctionName()
					+ "\" iconCls=\"" + function.getTSIcon().getIconClas()
					+ "\">");*/
			menuString.append("<div   title=\"" + function.getName()
					+ "\" data-options=\"iconCls:'" + function.getIconClas()
					+ "'\">");
			//int submenusize = function.getTSFunctions().size();
			int submenusize=function.getChildren().size()==0?0:1;
			if (submenusize == 0) {
				menuString.append("</div>");
			}
			if (submenusize > 0) {
				//menuString.append("<ul class=\"easyui-tree\"  fit=\"false\" border=\"false\">");
				menuString.append("<ul class=\"easyui-tree\">");
			}
			menuString.append(getChildOfTree(function.getChildren()));
			if (submenusize > 0) {
				menuString.append("</ul></div>");
			}
		}
		return menuString.toString();
	}
	/**
	 * 获取顶级菜单的下级菜单-----面板式菜单
	 * @param parent
	 * @param level
	 * @param map
	 * @return
	 */
	private static String getChild(List<MenuBean> list){
		StringBuffer menuString = new StringBuffer();
		for (MenuBean function : list) {
				int submenusize=function.getChildren().size()==0?0:1;
				if(submenusize==0){
					menuString.append(getLeaf(function));
				}else{
					menuString.append("<div  class=\"easyui-accordion\"  fit=\"false\" border=\"false\">");
					menuString.append("<div></div>");//easy ui 默认展开第一级,所以这里设置一个控制,就不展开了
					menuString.append("<div title=\"" + function.getName()
							+ "\" iconCls=\"" + function.getIconClas()
							+ "\"><ul>");
					menuString.append(getChild(function.getChildren()));
					menuString.append("</ul></div>");
					menuString.append("</div>");
				}
		}
		return menuString.toString();
	}
	/**
	 * 获取树形菜单
	 * @param parent
	 * @param level
	 * @param map
	 * @return
	 */
	private static String getChildOfTree(List<MenuBean> list){
		StringBuffer menuString = new StringBuffer();
		for (MenuBean function : list) {
				if(function.getIsMenu()!=null&&function.getIsMenu().equals("0"))
					continue;
				String ismenu=function.getIsMenu();
				String funcname=function.getName();
				EqualPredicate firstNameEqlPredicate = new EqualPredicate("1");
				BeanPredicate firtsNameBeanPredicate = new BeanPredicate("isMenu", firstNameEqlPredicate);
				Predicate[] allPredicateArray = { firtsNameBeanPredicate};
				Predicate allPredicate = PredicateUtils.allPredicate(allPredicateArray);
				int submenusize=function.getChildren().size()==0?0:1;
				 //Collection<TSFunction> filteredCollection =CollectionUtils.select(function.getTSFunctions(), allPredicate);
				//filteredCollection.isEmpty()||
				if(submenusize==0){
					menuString.append(getLeafOfTree(function));
				}else{
					menuString.append("<li state=\"closed\" iconCls=\"" + function.getIconClas()+"\" ><span>"+function.getName()+"</span>");
					menuString.append("<ul >");
					menuString.append(getChildOfTree(function.getChildren()));
					menuString.append("</ul></li>");
				}
		}
		return menuString.toString();
	}
	/**
	 * 获取叶子节点
	 * @param function
	 * @return
	 */
	private static String getLeaf(MenuBean function){
		StringBuffer menuString = new StringBuffer();
		String icon = "folder";
		if (function.getIconId() != null) {
			icon = function.getIconClas();
		}
		menuString.append("<li><div onclick=\"addTab(\'");
		menuString.append(function.getName());
		menuString.append("\',\'");
		menuString.append(function.getUrl());
		menuString.append("&clickFunctionId=");
		menuString.append(function.getNo());
		menuString.append("\',\'");
		menuString.append(icon);
		menuString.append("\')\"  title=\"");
		menuString.append(function.getName());
		menuString.append("\" url=\"");
		menuString.append(function.getUrl());
		menuString.append("\" iconCls=\"");
		menuString.append(icon);
		menuString.append("\"> <a class=\"");
		menuString.append(function.getName());
		menuString.append("\" href=\"#\" > <span class=\"icon ");
		menuString.append(icon);
		menuString.append("\" >&nbsp;</span> <span class=\"nav\" >");
		menuString.append(function.getName());
		menuString.append("</span></a></div></li>");
		return menuString.toString();
	}
	/**
	 * 获取叶子节点  ------树形菜单的叶子节点
	 * @param function
	 * @return
	 */
	private static String getLeafOfTree(MenuBean function){
		StringBuffer menuString = new StringBuffer();
		String icon = "folder";
		if (function.getIconId() != null) {
			icon = function.getIconClas();
		}
		menuString.append("<li iconCls=\"");
		menuString.append(icon);
		menuString.append("\"> <span><a onclick=\"addTab(\'");
		menuString.append(function.getName());
		menuString.append("\',\'");
		menuString.append(function.getUrl());
		menuString.append("&clickFunctionId=");
		menuString.append(function.getNo());
		menuString.append("\',\'");
		menuString.append(icon);
		menuString.append("\')\"  title=\"");
		menuString.append(function.getName());
		menuString.append("\" url=\"");
		menuString.append(function.getUrl());
		menuString.append("\" href=\"#\" ><span class=\"nav\" >");
		menuString.append(function.getName());
		menuString.append("</span></a></span></li>");
		return menuString.toString();
	}
	/**
	 * 拼装bootstrap头部菜单
	 * @param pFunctions
	 * @param functions
	 * @return
	 */
	public static String getBootstrapMenu(List<MenuBean> list) {
		StringBuffer menuString = new StringBuffer();
		menuString.append("<ul class=\"nav\">");
		if(list==null || list.size()==0){
			return "";
		}
		for (MenuBean pFunction : list) {
			int submenusize=pFunction.getChildren().size()==0?0:1;
			//是否有子菜单
			//boolean hasSub = pFunction.getTSFunctions().size()==0?false:true;
			boolean hasSub =submenusize>0?true:false;
			//绘制一级菜单
			menuString.append("	<li class=\"dropdown\"> ");
			menuString.append("		<a href=\"javascript:;\" class=\"dropdown-toggle\" data-toggle=\"dropdown\"> ");
			menuString.append("			<span class=\"bootstrap-icon\" style=\"background-image: url('"+pFunction.getIconPath()+"')\"></span> "+pFunction.getName()+" ");
			if(hasSub){
				menuString.append("			<b class=\"caret\"></b> ");
			}
			menuString.append("		</a> ");
			//绘制二级菜单
			if(hasSub){
				menuString.append(getBootStrapChild(pFunction.getChildren()));
			}
			menuString.append("	</li> ");
		}
		menuString.append("</ul>");
		return menuString.toString();
	}
	/**
	* @Title: getBootStrapChild
	* @Description: 递归获取bootstrap的子菜单
	*  @param parent
	*  @param level
	*  @param map
	* @return String    
	* @throws
	 */
	private static String getBootStrapChild(List<MenuBean> list){
		StringBuffer menuString = new StringBuffer();
		if(list==null || list.size()==0){
			return "";
		}
		menuString.append("		<ul class=\"dropdown-menu\"> ");
		for (MenuBean function : list) {
				int submenusize=function.getChildren().size()==0?0:1;
				//boolean hasSub = function.getTSFunctions().size()!=0 && map.containsKey(level+1);
				boolean hasSub =submenusize>0?true:false;
				String menu_url = function.getUrl();
				if(StringUtils.isNotEmpty(menu_url)){
					menu_url += "&clickFunctionId="+function.getNo();
				}
				menuString.append("		<li onclick=\"showContent(\'"+function.getName()+"\',\'"+menu_url+"\')\"  title=\""+function.getName()+"\" url=\""+function.getUrl()+"\" ");
				if(hasSub){
					menuString.append(" class=\"dropdown-submenu\"");
				}
				menuString.append(" > ");
				menuString.append("			<a href=\"javascript:;\"> ");
				menuString.append("				<span class=\"bootstrap-icon\" style=\"background-image: url('"+function.getIconPath()+"')\"></span>		 ");
				menuString.append(function.getName());
				menuString.append("			</a> ");
				if(hasSub){
					menuString.append(getBootStrapChild(function.getChildren()));
				}
				menuString.append("		</li> ");
		}
		menuString.append("		</ul> ");
		return menuString.toString();
	}

	
	/*public static String getDIYMultistageTree(List<MenuBean> list) {
		if(list==null||list.size()==0){return "不具有任何权限,\n请找管理员分配权限";}
		StringBuffer menuString = new StringBuffer();
        int curIndex = 0;
            for (MenuBean function : list) {
                menuString.append("<li>");
                menuString.append("<a href=\"#\" class=\"dropdown-toggle\" ><i class=\"menu-icon fa fa-desktop\"></i>")
                .append(function.getName());
                boolean hasSub=function.getChildren().size()==0?false:true;
                if(!hasSub){
                	menuString.append("</a></li>");
                	menuString.append(getDIYSubMenu(function));
                }else{
                	menuString.append("<b class=\"arrow\"></b><ul  class=\"submenu\" >");
                	menuString.append(getDIYSubMenu(function.getChildren()));
                	menuString.append("</ul></li>");
                }
                curIndex++;
            }

		return menuString.toString();
	}
	
	private static String getDIYSubMenu(List<MenuBean> list){
		StringBuffer menuString = new StringBuffer();
		for (MenuBean function : list) {
				boolean hasSub=function.getChildren().size()==0?false:true;
				if(!hasSub){
					menuString.append(getLeafOfDIYTree(function));
				}else{
					String icon = "folder";
					icon =function.getIconClas();
					menuString.append("<li><a href=\"#\" ><i class=\"menu-icon fa fa-eye pink\" iconCls=\"" 
							+ icon+"\" ></i>"+ function.getName() +"<b class=\"arrow\"></b>");
					menuString.append("<ul class=\"submenu\">");
					menuString.append(getChildOfTree(function.getChildren()));
					menuString.append("</ul></li>");
				}
		}
		return menuString.toString();
	}*/

	public static String getAceMultistageTree( List<MenuBean> list) {
		if(list==null||list.size()==0){return "不具有任何权限,\n请找管理员分配权限";}
		StringBuffer menuString = new StringBuffer();
        int curIndex = 0;
            for (MenuBean menu : list) {
                menuString.append("<li>");
                menuString.append("<a href=\"#\" class=\"dropdown-toggle\" ><i class=\"menu-icon fa fa-desktop\"></i>")
                .append(menu.getName());
                boolean hasSub=menu.getChildren().size()==0?false:true;
                if(!hasSub){
                	menuString.append("</a></li>");
                	menuString.append(getSubMenu(menu.getChildren()));
                }else{
                	menuString.append("</a><b class=\"arrow\"></b><ul  class=\"submenu\" >");
                	menuString.append(getSubMenu(menu.getChildren()));
                	menuString.append("</ul></li>");
                }
                curIndex++;
            }

		return menuString.toString();
	}
	private static String getSubMenu(List<MenuBean> list){
		StringBuffer menuString = new StringBuffer();
		for (MenuBean menu : list) {
				boolean hasSub=menu.getChildren().size()==0?false:true;
				if(!hasSub){
					menuString.append(getLeafOfACETree(menu));
				}else{
					String icon = "folder";
					icon=menu.getIcon();
					menuString.append("<li><a href=\"#\" ><i class=\"menu-icon fa fa-eye pink\" iconCls=\"" 
							+ icon+"\" ></i>"+ menu.getName() +"<b class=\"arrow\"></b>");
					menuString.append("<ul class=\"submenu\">");
					menuString.append(getChildOfTree(menu.getChildren()));
					menuString.append("</ul></li>");
				}
		}
		return menuString.toString();
	}
	private static String getLeafOfACETree(MenuBean menu){
		StringBuffer menuString = new StringBuffer();
		String icon = "folder";
		icon=menu.getShortcutClass();
		//addTabs({id:'home',title:'首页',close: false,url: 'loginController.do?home'});
		String name = menu.getName() ;
		menuString.append("<li iconCls=\"");
		menuString.append(icon);
		menuString.append("\"> <a href=\"javascript:addTabs({id:\'").append(menu.getNo());
		menuString.append("\',title:\'").append(name).append("\',close: true,url:\'");
		menuString.append(menu.getUrl());
		menuString.append("&clickFunctionId=");
		menuString.append(menu.getNo());
//		menuString.append("\',\'");
//		menuString.append(icon);
		menuString.append("\'})\"  title=\"");
		menuString.append(name);
		menuString.append("\" url=\"");
		menuString.append(menu.getUrl());
		menuString.append("\"  >");
		menuString.append(name);
		menuString.append("</a></li>");
		return menuString.toString();
	}
	
	private static String getLeafOfDIYTree(MenuBean menu){
		StringBuffer menuString = new StringBuffer();
		String icon = "folder";
		icon=menu.getShortcutClass();
		String name = menu.getName() ;
		menuString.append("<li iconCls=\"");
		menuString.append(icon);
		menuString.append("\"> <a href=\"javascript:loadModule(\'");
		menuString.append(name);
		menuString.append("\',\'");
		menuString.append(menu.getUrl());
		menuString.append("&clickFunctionId=");
		menuString.append(menu.getNo());
		menuString.append("\',\'");
		menuString.append(icon);
		menuString.append("\')\"  title=\"");
		menuString.append(name);
		menuString.append("\" url=\"");
		menuString.append(menu.getUrl());
		menuString.append("\"  >");
		menuString.append(name);
		menuString.append("</a></li>");
		return menuString.toString();
	}
}
